package com.sinopec.basemodule.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.sinopec.basemodule.R;
import com.sinopec.basemodule.model.ImageBean;
import com.sinopec.basemodule.utils.AndroidLifecycleUtils;
import com.sinopec.basemodule.view.PhotoPicker;

import java.util.ArrayList;

public class PhotoPickerAdapterBak extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private ArrayList<ImageBean> images;
    private LayoutInflater inflater;
    private Context context;
    private OnItemClickListener listener;

    public PhotoPickerAdapterBak(Context context, ArrayList<ImageBean> images, OnItemClickListener listener) {
        this.images = images;
        this.context = context;
        this.listener = listener;
        inflater = LayoutInflater.from(context);
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        switch (viewType) {
            case PhotoPicker.TYPE_ADD:
                holder = new AddViewHolder(inflater.inflate(R.layout.item_image_add, parent, false));
                break;
            default:
                holder = new PhotoViewHolder(inflater.inflate(R.layout.item_image, parent, false));
                break;
        }
        return holder;
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (images.get(position).getType() == PhotoPicker.TYPE_ADD) {
            AddViewHolder addViewHolder = (AddViewHolder) holder;
            addViewHolder.itemView.setOnClickListener(v -> {
                listener.itemClick(position);
            });
        } else {
            PhotoViewHolder photoViewHolder = (PhotoViewHolder) holder;
            Uri uri = images.get(position).getImageUri();
            if (uri == null) {
                photoViewHolder.delete.setVisibility(View.GONE);
                return;
            }
            photoViewHolder.delete.setVisibility(View.VISIBLE);
            boolean canLoadImage = AndroidLifecycleUtils.canLoadImage(photoViewHolder.ivPhoto.getContext());
            if (canLoadImage) {
                final RequestOptions options = new RequestOptions();
                options.centerCrop()
                        .placeholder(R.drawable.ic_rectangle_picture_loading)
                        .error(R.drawable.ic_rectangle_picture_error)
                        .diskCacheStrategy(DiskCacheStrategy.NONE)
                        .skipMemoryCache(true);
                Glide.with(context)
                        .load(uri)
                        .apply(options)
                        .thumbnail(0.1f)
                        .into(photoViewHolder.ivPhoto);
            }
            photoViewHolder.itemView.setOnClickListener(v -> {
                listener.itemClick(position);
            });
            photoViewHolder.delete.setOnClickListener(v -> {
                listener.deleteClick(position);
            });
        }
    }

    @Override
    public int getItemViewType(int position) {
        return images.get(position).getType();
    }

    @Override
    public int getItemCount() {
        return images.size();
    }

    public static class PhotoViewHolder extends RecyclerView.ViewHolder {
        private ImageView ivPhoto;
        private View delete;

        public PhotoViewHolder(View itemView) {
            super(itemView);
            ivPhoto = itemView.findViewById(R.id.iv_photo);
            delete = itemView.findViewById(R.id.iv_delete);
        }
    }

    public static class AddViewHolder extends RecyclerView.ViewHolder {

        public AddViewHolder(View itemView) {
            super(itemView);
        }
    }

    public interface OnItemClickListener {
        void deleteClick(int position);

        void itemClick(int position);
    }
}
